
////#include "MSCorEE.h"
//#include <metahost.h>
//#pragma comment(lib, "mscoree.lib")
//
//void StartTheDotNetRuntime()
//{
//	ICLRMetaHost       *pMetaHost       = NULL;
//
//	if(CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost, (LPVOID*)&pMetaHost) != S_OK || !pMetaHost)
//	{
//		TraceError(TraceUnDefine, L"CLRCreateInstance fail");
//		return;
//	}
//	
//	IEnumUnknown * pEnum = NULL;
//	if(pMetaHost->EnumerateLoadedRuntimes(GetCurrentProcess(), &pEnum) != S_OK || ! pEnum)
//	{
//		TraceError(TraceUnDefine, L"EnumerateLoadedRuntimes fail");
//		return;
//	}
//	for (;;)
//	{
//		IUnknown * pRuntime;
//		ULONG cRuntimes;
//		if(pEnum->Next(1, &pRuntime, &cRuntimes) != S_OK || ! pRuntime)
//		{
//			TraceError(TraceUnDefine, L"Next fail");
//			return;
//		}
//		ICLRRuntimeInfo * pRuntimeInfo;
//		if(pRuntime->QueryInterface(IID_ICLRRuntimeInfo, (LPVOID *)&pRuntimeInfo) != S_OK || ! pRuntimeInfo)
//		{
//			TraceError(TraceUnDefine, L"pRuntime->QueryInterface fail");
//			return;
//		}
//		pRuntime->Release();
//
//		WCHAR wszVersion[500];
//		DWORD cchVersion = _countof(wszVersion);
//		pRuntimeInfo->GetVersionString(wszVersion, &cchVersion);
//		TraceError(TraceUnDefine, L"%S\n", wszVersion);
//
//		ICLRRuntimeHost *pClrRuntimeHost = NULL;
//		if(pRuntimeInfo->GetInterface(CLSID_CLRRuntimeHost, IID_PPV_ARGS(&pClrRuntimeHost)) != S_OK || ! pClrRuntimeHost)
//		{
//			TraceError(TraceUnDefine, L"pRuntimeInfo->GetInterface fail");
//			return;
//		}
//		if(FAILED(pClrRuntimeHost->Start()))
//		{
//			TraceError(TraceUnDefine, L"pClrRuntimeHost->Start fail");
//			return;
//		}
//		DWORD dwRet = 0;
//		pClrRuntimeHost->ExecuteInDefaultAppDomain(
//			L"E:\\_W\\ce 4.41\\Injectee\\bin\\Debug\\Injectee.dll",
//			L"Injectee.UserControl1", L"MyMethod", L"MyParameter", &dwRet);
//		pClrRuntimeHost->Stop();
//		pRuntimeInfo->Release();
//	}
//
//    //// Bind to the CLR runtime..
//    //ICLRRuntimeHost *pClrHost = NULL;
//    //hr = CorBindToRuntimeEx(
//    //    NULL, L"wks", 0, CLSID_CLRRuntimeHost,
//    //    IID_ICLRRuntimeHost, (PVOID*)&pClrHost);
//
//    //// Push the big START button shown above
//    //hr = pClrHost->Start();
//
//    //// Okay, the CLR is up and running in this (previously native) process.
//    //// Now call a method on our managed C# class library.
//    //DWORD dwRet = 0;
//    //hr = pClrHost->ExecuteInDefaultAppDomain(
//    //    L"E:\\_W\\ce 4.41\\Injectee\\bin\\Debug\\Injectee.dll",
//    //    L"Injectee.UserControl1", L"MyMethod", L"MyParameter", &dwRet);
//
//    //// Optionally stop the CLR runtime (we could also leave it running)
//    //hr = pClrHost->Stop();
//
//    //// Don't forget to clean up.
//    //pClrHost->Release();
//}
package sinica.googlefileservice.server.datastore;

import java.util.List;

import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.jdo.Transaction;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;


import sinica.googlefileservice.server.datastore.util.PMF;



@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class TitleInfo {
	public final static String xmlFile = "xml";
	public final static String imageFile = "img";
	public final static String audioFile = "ado";
	public final static String authorDatafile = "adt";
	@PrimaryKey
	@Persistent
	private String id;
	@Persistent
	private String name;
	@Persistent
	private String desciption;
	@Persistent
	private String author;
	@Persistent
	private String copyRight;
	@Persistent
	long permission;
	@NotPersistent
	private IAFile xml;
	@NotPersistent
	private IAFile image;
	@NotPersistent
	private IAFile audio;
	@NotPersistent
	private IAFile authorData;
	
	public TitleInfo(String id, String name, String description, String author, String copyright, 
			long permission, IAFile xml, IAFile image, IAFile audio, IAFile authorData) {
		this.id = id;
		this.name = name;
		this.desciption = description;
		this.author = author;
		this.copyRight = copyright;
		this.permission = permission;
		this.xml = xml;
		this.image = image;
		this.audio = audio;
		this.setAuthorData(authorData);
	}
	
	public TitleInfo(String id, String name, String description, String author, String copyright, long permission, 
			List<GoogleUnit> xml, List<GoogleUnit> image, List<GoogleUnit> audio,List<GoogleUnit> authorData) {
		this.id = id;
		this.name = name;
		this.desciption = description;
		this.author = author;
		this.copyRight = copyright;
		this.permission = permission;
		this.xml = new IAFile(id+xmlFile, xml);
		this.image = new IAFile(id+imageFile, image);
		this.audio = new IAFile(id+audioFile, audio);
		this.setAuthorData(new IAFile(id+audioFile, authorData));
	}
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDescription() {
		return desciption;
	}
	public void setDescription(String description) {
		this.desciption = description;
	}
	public String getAuthor() {
		return author;
	}
	
	public void setAuthor(String author) {
		this.author = author;
	}
	public String getCopyRight() {
		return copyRight;
	}
	public void setCopyRight(String copyRight) {
		this.copyRight = copyRight;
	}
	public long getPermission() {
		return permission;
	}
	public void setPermissionRight(long permission) {
		this.permission = permission;
	}
	public void setXml(IAFile xml) {
		this.xml = xml;
	}
	public IAFile getXml() {
		if(xml != null)
			return xml;
		return xml = IAFile.load(id+xmlFile);
	}
	public void setImage(IAFile image) {
		this.image = image;
	}

	public IAFile getImage() {
		if(image != null)
			return image;
		return image = IAFile.load(id + imageFile);
	}

	public void setAudiio(IAFile audio) {
		this.audio = audio;
	}

	public IAFile getAudio() {
		if(audio != null)
			return audio;
		return audio = IAFile.load(id + audioFile);
	}
	public void setAuthorData(IAFile authorData) {
		this.authorData = authorData;
	}

	public IAFile getAuthorData() {
		return authorData;
	}
	public static int save(TitleInfo ti)
	{
		if(ti.xml != null)
			IAFile.save(ti.xml);
		if(ti.image != null)
			IAFile.save(ti.image);
		if(ti.audio != null)
			IAFile.save(ti.audio);
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		try {
			tx.begin();
			pm.makePersistent(ti);
			tx.commit();
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
			pm.close();
		}
		return 0;
	}
	public static int delete(TitleInfo ti)
	{
		IAFile.delete(ti.xml);
		IAFile.delete(ti.image);
		IAFile.delete(ti.audio);
		IAFile.delete(ti.authorData);
		int  rt = -1;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.deletePersistent(ti);
			rt = 0;
		} catch (JDOObjectNotFoundException e) {
		} finally {
			pm.close();
		}
		return rt;
	}
	public static int delete(String id)
	{
		IAFile.delete(id+xmlFile);
		IAFile.delete(id+imageFile);
		IAFile.delete(id+audioFile);
		IAFile.delete(id+authorDatafile);
		int rt = -1;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			TitleInfo ti = pm.getObjectById(TitleInfo.class, id);
			pm.deletePersistent(ti);
			rt = 0;
		} catch (JDOObjectNotFoundException e) {
    		//no entities in Google datastore
		} finally {
			pm.close();
		}
		return rt;
	}
	
	public static TitleInfo load(String id, boolean loadFiles)
	{
		TitleInfo ti = null;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			ti = pm.getObjectById(TitleInfo.class, id);
			
		} catch (JDOObjectNotFoundException e) {
		} finally {
			pm.close();
		}
		if(loadFiles)
		{
			ti.xml = IAFile.load(id+xmlFile);
			ti.image = IAFile.load(id+imageFile);
			ti.audio = IAFile.load(id+audioFile);
			ti.authorData = IAFile.load(id+authorDatafile);
		}
		return ti;
	}
}